<?php
class galaxyAction extends Action {	
	var $contentPage;
	function __construct() {
		set_exception_handler(array('galaxyAction','gException'));
		parent::__construct();
		galaxy::getInstance();
		import('gTpl', TEMPLATE_PATH);
	}
	protected function display($page = '') {
		//$trace = debug_backtrace();
		//$this->contentPage = '..:defualt:'.substr($trace[1]['class'],0, -6).'_'.$trace[1]['function'];
		if($_GET['ajax']) $this->ajaxReturn(parent::fetch($this->get('contentPage')), 'ok', 1);
		parent::display($page ? $page : $this->get('contentPage'));
	}
	/**
	 * show a message and jump to $fwd
	 * @param $msg the message
	 * @param $fwd the url(empty to go back)
	 * @return void
	 */
	protected function showMessage($msg, $fwd='', $e=null) {
		if(preg_match('/^[a-zA-Z0-9_\-\s]+$/',$msg)) $msg = L($msg);
		$this->view->assign(get_defined_vars());
		if($_GET['ajax']) $this->ajaxReturn($fwd, $msg, 2);
		parent::display('..:default:public_message');
		exit;
	}
	/**
	 * whether to ignore checksubmit
	 * @var bool
	 */
	public $ignore_checksubmit = false;
	/**
	 * check the submit if it's a illegal one
	 * @param bool $ignore_sec = false whether to ignore seccode
	 * @param array $data = $_POST the submit data
	 * @return $this
	 */
	protected function checksubmit($ignore_sec = false, &$data = '') {
		if($this->ignore_checksubmit) return;
		is_array($data) or $data = &$_POST;
		count($data)>0 or galaxy::gthrow('bad_submit');
		galaxy::checkToken($data) or galaxy::gthrow('bad_submit');;
		$ignore_sec or gSeccode::check($data) or galaxy::gthrow('wrong_seccode');
		return $this;
	}
	protected function checkLogin() {
		if($_ENV['g']->uid <= 0) $this->showMessage('login_please',array('delay'=>2000, 'run'=>'$.gx.openDialog("'.U('core-logging/').'","login",{title:\''.L('user').L('login').'\',width:400,height:300})'));
		return $this;	
	}
	
	public static function gException($E) {
		if($E instanceof galaxyException) {
			$error = $E->getError();
			A('galaxy')->showMessage($error['message'], $error['fwd'], $error['e']);
		} else {
			halt($E->__toString());
		}
	}
}
?>